from flask import Blueprint, jsonify, request
import json
from db import db1

bp_day01 = Blueprint('day01', __name__)


@bp_day01.route('/yewu', methods=['POST', 'GET', 'PUT'])
def yewu():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = """insert into yewu values (%d,"%s","%s")""" % (0, data['name'], "")
        db1.update(sql)
        return jsonify({'code': 200})

    elif request.method == 'GET':
        sql = """select * from yewu"""
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})

    elif request.method == 'PUT':
        id = request.args.get('id')
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form

        print(data)
        for i in data:
            sql = """update yewu set parms = "%s" where id = %d """ % (i, int(id))
            db1.update(sql)
        return jsonify({'code': 200})
